package com.my.blogs.personblog.Service.ServiceImpl;

import com.my.blogs.personblog.Repository.UserRepository;
import com.my.blogs.personblog.Service.NlpService;
import com.my.blogs.personblog.Service.UserService;
import com.my.blogs.personblog.enity.User;
import com.my.blogs.personblog.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRepository userRepository;
    @Override
    public User login(String username, String password) {
        if (username == null || username.length() < 8 || username.length() > 10 || !username.matches("[a-zA-Z]+")) {
            throw new IllegalArgumentException("用户名格式错误，请使用字母进行组合（8~10个字符）");
        }
        if (password == null || password.length() < 8 || password.length() > 16 || !password.matches("[a-zA-Z0-9_]+")) {
            throw new IllegalArgumentException("密码格式错误，请使用字母、数字、下划线（8~16个字符）");
        }

        // 调用userMapper.login(username, password)进行登录验证
        User user = userMapper.login(username, password);
        if (user == null) {
            throw new IllegalArgumentException("用户名或密码错误");
        }
        return user;
    }

    @Override
    public User register(String username, String password, String email, String phone, String IdCard) {
        // 验证用户名格式
        if (username == null || username.length() < 8 || username.length() > 10 || !username.matches("[a-zA-Z]+")) {
            throw new IllegalArgumentException("用户名格式错误，请使用字母进行组合（8~10个字符）");
        }

        // 验证密码格式
        if (password == null || password.length() < 8 || password.length() > 16 || !password.matches("[a-zA-Z0-9_]+")) {
            throw new IllegalArgumentException("密码格式错误，请使用字母、数字、下划线（8~16个字符）");
        }

        // 验证电子邮件格式
        if (email == null || email.length() < 8 || email.length() > 16 || !email.matches("[a-zA-Z]+")) {
            throw new IllegalArgumentException("电子邮件格式错误，请使用字母EMAIL进行组合（8~16个字符）");
        }

        // 验证身份证号码格式
        if (IdCard == null || IdCard.length() < 15 || IdCard.length() > 18 || !IdCard.matches("[0-9]+")) {
            throw new IllegalArgumentException("身份证号码格式错误，请使用数字进行组合（15~18位）");
        }

        // 验证手机号码格式
        if (phone == null || phone.length() != 11 || !phone.matches("[0-9]+")) {
            throw new IllegalArgumentException("手机号码格式错误，请使用数字进行组合（11位）");
        }

        // 检查用户名是否已存在
        if (UserMapper.existsByUsername(username)) {
            throw new IllegalArgumentException("用户名已存在");
        }

        // 调用userMapper.register(username, password, email, phone, IdCard)进行注册
        User newUser = userMapper.register(username, password, email, phone, IdCard);
        if (newUser == null) {
            throw new RuntimeException("注册失败");
        }
        return newUser;
    }

    @Override
    public List<User> selectAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public User selectUserById(int id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public User selectUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public User selectUserByPhone(String phone) {
        return userMapper.selectUserByPhone(phone);
    }

    @Override
    public Boolean insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public Boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Boolean deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public List<User> sortById(int id) {
        return userMapper.sortById(id);
    }

    @Override
    public List<User> selectPage(int page, int pageSize) {
        return userMapper.selectPage(page, pageSize);
    }
}
